#include "tcp_socket.hpp"

#define CHECE_ERS(q) if((q)==false){return -1;}



int main(int argc,char* argv[])
{
  if(argc!=3)
  {
    std::cout<<"Usage :./tcp_cli 192.168.12 9000"<<std::endl;
    std::cout<<"Server Address!"<<std::endl;
      return -1;
  }
  std::string srv_ip=argv[1];
  int srv_prot=std::stoi(argv[2]);

//创建套接字
  TcpSocket cli_sock;
  CHECE_ERS(cli_sock.Socket());

  //客户端不推荐主动绑定字符串
  ////向服务器发起链接
  CHECE_ERS(cli_sock.Connect(srv_ip,srv_prot));
  while(1)
  {
    //与服务器通信
    std::cout<<"client say:";
    fflush(stdout);
    std::string buf;
    std::cin>>buf;

    int ret;
    ret=cli_sock.Send(buf);
    if(ret==false)
    {
      cli_sock.Close();
      return -1;
    }

    buf.clear();
    ret=cli_sock.Recv(&buf);
    if(ret==false)
    {
      cli_sock.Close();
      return -1;
    }
    std::cout<<"server say:"<<buf;
  }
  //关闭套接字
  CHECE_ERS(cli_sock.Close());
  return 0;

}
